<template>
    <el-card class="w-full pt-[5px]">
        <!-- <el-radio-group v-model="planType">
            <el-radio-button label="全部" value="" />
            <el-radio-button label="审批通过" value="0" />
            <el-radio-button label="审批不通过" value="1" />
            <el-radio-button label="审批中" value="2" />
        </el-radio-group> -->
        <el-form :model="searchForm" class="mt-[20px]" label-width="110px">
            <el-row :gutter="20">
                <el-col :span="5">
                    <el-form-item label="编号" class="mb-[0px]">
                        <el-input v-model="searchForm.code" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="名称" class="mb-[0px]">
                        <el-input v-model="searchForm.name" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="论文类型">
                        <el-select v-model="searchForm.paperModeId" clearable>
                            <el-option v-for="item in lwlx" :key="item.code" :label="item.name"
                                :value="item.code"></el-option>

                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="是否为收录">
                        <el-select v-model="searchForm.isEmbody" clearable>
                            <el-option label="是" value="1"></el-option>
                            <el-option label="否" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>


                <el-col :span="2">
                    <div class="flex justify-end">
                        <el-button type="primary" @click="getList(1)">查询</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>
    </el-card>
    <el-card class="w-full pt-[5px] mt-[10px]">
        <div class="mb-[20px]">
            <el-button type="primary" @click="handleAdd">
                <el-icon class="mr-[5px]">
                    <Plus />
                </el-icon>
                新增
            </el-button>
            <el-button @click="handleDel">
                <el-icon>
                    <DeleteFilled />
                </el-icon>
                删除
            </el-button>
        </div>
        <el-table :data="tableData" class="w-full" @selection-change="handleSelectionChange" stripe>
            <el-table-column type="selection" width="55" />
            <el-table-column prop="code" label="编号"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="paperModeId" label="论文类型" :formatter="formatlwlx" />
            <el-table-column prop="beginDate" label="进入期刊源" show-overflow-tooltip>

                <template #default="scope">
                    <template v-if="scope.row.magazineSourceIds">
                        <el-link type="primary">
                            <span v-for="(item, index) in scope.row.magazineSourceIds.split(',')"
                                @click="goqky(scope.row.id)" :key="item">{{
            formatqky(item, (index + 1) != scope.row.magazineSourceIds.split(',').length)
        }}</span>
                        </el-link>
                    </template>
                    <template v-else><el-link type="primary" @click="goqky(scope.row.id)">/</el-link></template>
                </template>
            </el-table-column>
            <el-table-column prop="isEmbody" label="是否收录">
                <template #default="scope"> {{ scope.row.isEmbody == 1 ? '是' : '否' }} </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="300" align="center">
                <template #default="scope">
                    <el-button type="primary" size="small" plain @click="handleEdit(scope.row)">
                        编辑
                    </el-button>
                    <el-button type="danger" size="small" plain @click="delHonor(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-[20px] flex justify-end">
            <pagination v-show="total > 0" :total="total" v-model:page="pageInfo.pageNum"
                v-model:limit="pageInfo.pageSize" @pagination="getList" />
        </div>
    </el-card>
    <EditModal v-model="editModelVisible" ref="EditModalRef" :lwlx="lwlx" :lwqkytableData="lwqkytableData"
        @callBack="callBackFun" />
</template>

<script setup>
import { onMounted } from 'vue';
import { getCategory } from "@/api/system/category"
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { getlevelList, levelDel, getlevelInfo, getsourceList } from "@/api/achievement/kycg"
import EditModal from "./EditModal.vue";
const router = useRouter();
const planType = ref("");
const searchFormOpen = ref(false);
const editModelVisible = ref(false);
const ViewdialogVisible = ref(false);
const searchForm = ref({
    name: "",
    code: "",
    isEmbody: "",
    paperModeId: "",
});
const pageInfo = ref({
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = ref([]);
//点击新增
const handleAdd = () => {
    editModelVisible.value = true;
};
const EditModalRef = ref(null)
//点击编辑
const handleEdit = (i) => {
    getlevelInfo(i.id).then(res => {
        if (res.code == 200) {
            editModelVisible.value = true;
            EditModalRef.value.getRuleForm(res.data)
        }
    })
};



const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
//点击删除
const handleDel = () => {
    if (multipleSelection.value.length == 0) {
        ElMessage.warning("请选择要删除的数据")
        return;

    } else {
        ElMessageBox.confirm("是否确认删除？", "提示", {
            type: "warning",
        }).then(() => {
            let ids = multipleSelection.value.map(item => item.id)
            ids = ids.join(',')
            levelDel(ids).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })
        });
    }

};

const lwlx = ref([])
const getCategoryMet = async () => {
    let parms = {
        bizName: "论文类型"
    }
    let res = await getCategory(parms)
    if (res.code == 200) {
        lwlx.value = res.data
    }
}
const lwqkytableData = ref([])
const getlwqkyList = async () => {
    let parms = {
        pageNum: 1,
        pageSize: 100
    }
    let res = await getsourceList(parms)
    if (res.code == 200) {
        lwqkytableData.value = res.rows
    }
}
const formatqky = (i, j) => {
    let qkyName = lwqkytableData.value.find(item => item.id == i) ? lwqkytableData.value.find(item => item.id == i).name : ''
    if (j && qkyName) {
        return qkyName + ','
    }
    return qkyName
}
const goqky = (id) => {
    router.push({
        path: "/achievement/kycg/lwqkysz/index",
        query: { id: id }
    })
}
const formatlwlx = (row, column, cellValue, index) => {
    // if (lwlx.value.length && cellValue) {
    //     if (lwlx.value.find(item => item.code == cellValue)) {
    //         return lwlx.value.find(item => item.code == cellValue).name
    //     } else {
    //         return ''
    //     }

    // } else {
    //     return ''
    // }
    cellValue = cellValue.split(',')
    let zcArr = []
    if (lwlx.value.length && cellValue && cellValue.length > 0) {
        for (let i = 0; i < cellValue.length; i++) {
            zcArr[i] = lwlx.value.find(item => item.code == cellValue[i]) ? lwlx.value.find(item => item.code == cellValue[i]).name : null
        }
        zcArr = Array.from(zcArr).filter(function (element) {
            return element !== undefined && element !== null;
        });
        return zcArr.join('、')
    } else {
        return ''
    }
}

const initList = () => {
    Promise.all([getCategoryMet()]).then(() => {
        getList()
    })
}

const getList = async (i) => {
    if (i == 1) {
        pageInfo.value.pageNum = 1
    }
    let parms = {
        ...searchForm.value,
        ...pageInfo.value
    }
    let res = await getlevelList(parms)
    if (res.code == 200) {
        tableData.value = res.rows
        total.value = res.total
        console.log(tableData.value)
    }
}
const callBackFun = () => {
    getList()
    getlwqkyList()
}
const delHonor = (i) => {
    ElMessageBox.confirm(
        '确认！删除这条数据?',
        '提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            levelDel(i.id).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })


        })
        .catch(() => {

        })

}


onMounted(() => {
    initList()
    // getCategoryMet2()
    getlwqkyList()
});
</script>

<style scoped lang="scss"></style>